﻿/**
 * Game.cs - Game logic
 * 
 * Part of BoulderDAM - A BoulderDash clone using C# and SDL
 * Nacho Cabanes & students of DAM IES San Vicente, 2014
 * 
 * Revisions
 * #     Date         Creator, changes
 * ----  -----------  -------------------------------------------------------
 * 0.01  02-apr-2014  Nacho Cabanes: Skeleton with game loop
 * 0.02  03-apr-2014  Alejandro Marin Pons, Jose Valdes, Jose Estrada: 
 *                        CheckInputDevices allows movement of Player, 
 *                        When Player has 0 lives game ends
 * 0.03  03-apr-2014  Nacho Cabanes: Basic joystick/gamepad support, 
 *                        god-mode to allow advancing levels (T+L)
 * 
 */

namespace Boulder
{
    class Game
    {
        bool gameFinished;
        Player myPlayer;
        Level level;
        ScoreBoard scoreBoard;
        int score;

        public void Run()
        {
            gameFinished = false;
            myPlayer = new Player();
            level = new Level();
            scoreBoard = new ScoreBoard();
            score = 0;
            while (!gameFinished)
            {
                DrawElements();
                CheckInputDevices();
                MoveElements();
                CheckLogic();
                PauseTillNextFrame();
            } // end of game loop
        }

        public void DrawElements()
        {
            Hardware.ClearScreen();
            level.DrawOnHiddenScreen();
            myPlayer.DrawOnHiddenScreen();
            Hardware.ShowHiddenScreen();
        }


        public void CheckInputDevices()
        {
            // Movement: arrow keys or joystick

            if ((Hardware.KeyPressed(Hardware.KEY_LEFT)) 
                    || (Hardware.JoystickMovedLeft())) 
                myPlayer.MoveLeft();

            if ((Hardware.KeyPressed(Hardware.KEY_RIGHT))
                    || (Hardware.JoystickMovedRight())) 
                myPlayer.MoveRight();

            if ((Hardware.KeyPressed(Hardware.KEY_UP))
                    || (Hardware.JoystickMovedUp())) 
                myPlayer.MoveUp();

            if ((Hardware.KeyPressed(Hardware.KEY_DOWN))
                    || (Hardware.JoystickMovedDown())) 
                myPlayer.MoveDown();

            // Hidden god-mode keys

            if ((Hardware.KeyPressed(Hardware.KEY_T)) &&
              (Hardware.KeyPressed(Hardware.KEY_L)))
                level.Advance();

            // ESC to return to Intro

            if (Hardware.KeyPressed(Hardware.KEY_ESC))
                gameFinished = true;

            // TO DO: To be completed
        }


        public void MoveElements()
        {
            level.AnimateElements();
        }

        //
        public void CheckLogic()
        {
            if (myPlayer.GetLives() == 0)
            {
                gameFinished = true;
            }

            //if (myPlayer.IsDead())
            //{
            //    myPlayer.LooseLife();
            //}
            // TO DO
        }

        public static void PauseTillNextFrame()
        {
            Hardware.Pause(20);  // 50 fps
        }
    }
}